from model_dummyserver import *
#start_stop tests are for existing applications/solutions
#configuration Primary and Backup LocalTserver, Solution consists of these 2 tservers

hostName = "mashaf"
LCAPort = 4999

def start_stop_1():
  ScsSolution1.Stop()
  ScsSolution1.Start()
  
def start_stop_2():
  ScsLocalTserver.Stop()
  ScsLocalTserver.Start() 
  ScsLocalTserverBackup.Stop()
  ScsLocalTserverBackup.Start() 
  ScsLocalTserver.Switchover()
  

def _createCfgAppDummyP():
  #supplementary procedure
  CfgHostDummy = CfgHost(hostName)
  
  ServerInfoDummyP = CfgServerInfo()
  
  ServerInfoDummyP.SetAttributes({"hostDBID": CfgHostDummy.DBID,
                                  "port": "1234"})
  
  CfgAppDummyP = CfgApplication()                                
  CfgAppDummyP.SetAttributes({"name": "DUMMY_P", 
                           "type": CfgAppType.CFGTServer.val,
                           "version": "0",
                           "tenantDBIDs": [1],
                           "isServer": CfgFlag.True.val,
                           "serverInfo": ServerInfoDummyP,
                           "startupTimeout": 10,
                           "shutdownTimeout": 10,
                           "redundancyType": CfgHAType.CFGHTHotStanby.val,
                           "workDirectory": "./",
                           "commandLine": "./",
                           "commandLineArguments": "./"})
  
  CfgAppDummyP.Add()
  return CfgAppDummyP

def _createCfgAppDummyB():
  #supplementary procedure
  CfgHostDummy = CfgHost(hostName)

  ServerInfoDummyB = CfgServerInfo()
  ServerInfoDummyB.SetAttributes({"hostDBID": CfgHostDummy.DBID,
                                  "port": "4321"})
  
  CfgAppDummyB = CfgApplication()                                
  CfgAppDummyB.SetAttributes({"name": "DUMMY_B", 
                           "type": CfgAppType.CFGTServer.val,
                           "version": "0",
                           "tenantDBIDs": [1],
                           "isServer": CfgFlag.True.val,
                           "serverInfo": ServerInfoDummyB,
                           "startupTimeout": 10,
                           "shutdownTimeout": 10,
                           "redundancyType": CfgHAType.CFGHTHotStanby.val,
                           "workDirectory": "./",
                           "commandLine": "./",
                           "commandLineArguments": "./"})  
  CfgAppDummyB.Add()
  

  return CfgAppDummyB


  
def lca_and_scs_dummy_t1():
  #create new apps
  CfgAppDummyP = _createCfgAppDummyP()
  CfgAppDummyB = _createCfgAppDummyB()
  
  CfgAppDummyP.BeginChange()
  CfgAppDummyP.serverInfo.backupServerDBID = CfgAppDummyB.DBID
  CfgAppDummyP.EndChange()

  try:
    ScsDummyP = ScsApplication(CfgAppDummyP)
    DSP = DummyServer("DUMMY_P", LCAPort)
    
    ScsDummyB = ScsApplication(CfgAppDummyB)
    DSB = DummyServer("DUMMY_B", LCAPort)
    
    #starting DUMMY_P
    DSP.ConnectToLCA()
    time.sleep(1)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_INITIALIZING) # start as backup

    
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_INITIALIZING)
   
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_SERVICE_UNAVAILABLE)
    DSP.ChangeExecutionMode(AppRunMode.APP_RUNMODE_PRIMARY)
    DSP.ConfirmChangeExecutionMode()
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_SERVICE_UNAVAILABLE)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_RUNNING)
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_RUNNING)

    #starting DUMMY_B
    DSB.ConnectToLCA()
    time.sleep(1)
    DSB.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_INITIALIZING)
    
    ScsDummyB.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_INITIALIZING)

    DSB.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_SERVICE_UNAVAILABLE)
    DSB.ChangeExecutionMode(AppRunMode.APP_RUNMODE_PRIMARY)
    DSB.ConfirmChangeExecutionMode()
    ScsDummyB.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_SERVICE_UNAVAILABLE)
    DSB.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_RUNNING)
    ScsDummyB.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_RUNNING)
    # should be changed to backup
    DSB.ConfirmChangeExecutionMode()
    
    #check
    SolutionControlServer.testValue(DSB.GetExecutionMode(), AppRunMode.APP_RUNMODE_BACKUP, "Incorrect run mode for DSB")
    SolutionControlServer.testValue(DSP.GetExecutionMode(), AppRunMode.APP_RUNMODE_PRIMARY, "Incorrect run mode for DSP")
    
    time.sleep(3)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_STOP_TRANSITION)
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_STOP_TRANSITION)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_STOPPED)
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_STOPPED)
    DSP.DisconnectFromLCA()
    DSB.ConfirmChangeExecutionMode()
    DSB.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_STOP_TRANSITION)
    ScsDummyB.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_STOP_TRANSITION)
    DSB.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_STOPPED)
    ScsDummyB.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_STOPPED)    
    DSB.DisconnectFromLCA()
    
  finally:
    CfgAppDummyP.Delete()
    CfgAppDummyB.Delete()
    ScsDummyP = None
    ScsDummyP = None


def test_alarm1():
  CfgAppDummyP = _createCfgAppDummyP()
  
  try:
    AppFailureAlarm = ScsAlarmCondition("Application Failure")
    AppFailureAlarm.Unmask()
    DSP = DummyServer("DUMMY_P", LCAPort)
    DSP.ConnectToLCA()
    time.sleep(1)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_INITIALIZING) # start as backup
    time.sleep(1)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_STOPPED) # emulate app failure, should raise alarm
    AppFailureAlarm.Detected(CfgAppDummyP)
    AppFailureAlarm.Deactivate(CfgAppDummyP)
    AppFailureAlarm.Mask()
    DSP.DisconnectFromLCA()
  finally:
    CfgAppDummyP.Delete()
    
def test_alarm2():
  """uses another deactivate (by alarm DBID only)"""
  CfgAppDummyP = _createCfgAppDummyP()
  
  try:
    AppFailureAlarm = ScsAlarmCondition("Application Failure")
    AppFailureAlarm.Unmask()
    DSP = DummyServer("DUMMY_P", LCAPort)
    DSP.ConnectToLCA()
    time.sleep(1)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_INITIALIZING) # start as backup
    time.sleep(1)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_STOPPED) # emulate app failure, should raise alarm
    AppFailureAlarm.Detected(CfgAppDummyP)
    AppFailureAlarm.Deactivate()
    AppFailureAlarm.Mask()
    DSP.DisconnectFromLCA()
  finally:
    CfgAppDummyP.Delete()
    
   

def test_log_1():
  #create new app
  CfgAppDummyP = _createCfgAppDummyP()

  try:
    ScsDummyP = ScsApplication(CfgAppDummyP)
    DSP = DummyServer("DUMMY_P", LCAPort)
    
    lo = LogObject(CfgAppDummyP)
    
    #"start" DUMMY_P
    DSP.ConnectToLCA()
    time.sleep(1)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_INITIALIZING) # start as backup

    
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_INITIALIZING)
   
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_SERVICE_UNAVAILABLE)
    DSP.ChangeExecutionMode(AppRunMode.APP_RUNMODE_PRIMARY)
    DSP.ConfirmChangeExecutionMode()
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_SERVICE_UNAVAILABLE)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_RUNNING)
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_RUNNING)
   
    #check
    SolutionControlServer.testValue(DSP.GetExecutionMode(), AppRunMode.APP_RUNMODE_PRIMARY, "Incorrect run mode for DSP")
    # send  message to MS
    
    lo.RequestLogMessage("CTI Link disconnected", LogLevel.LOG_LEVEL_ERROR, LogCategory.LOG_CATEGORY_APP, 20002)
    
    #"stop" DUMMY_P
    time.sleep(3)
    raw_input()
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_STOP_TRANSITION)
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_STOP_TRANSITION)
    DSP.UpdateStatus(AppLiveStatus.SCS_APP_STATUS_STOPPED)
    ScsDummyP.CheckUpdate(expectedStatus = AppLiveStatus.SCS_APP_STATUS_STOPPED)
    DSP.DisconnectFromLCA()

  finally:
    CfgAppDummyP.Delete()

  

    
  
  

  
  